package vip.xiaonuo.hontu.listener.handle.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.hontu.listener.handle.HandleService;
import vip.xiaonuo.hontu.listener.handle.model.HongTuPushData;
import vip.xiaonuo.hontu.listener.handle.model.SecurityPushData;
import vip.xiaonuo.modular.htpushdata.entity.HtPushData;
import vip.xiaonuo.modular.htpushdata.service.HtPushDataService;
import vip.xiaonuo.modular.securitydata.entity.SecurityData;
import vip.xiaonuo.modular.securitydata.service.SecurityDataService;

import javax.annotation.Resource;

@Service
public class SecurityPushDataHandleService implements HandleService<SecurityPushData> {

    @Resource
    private HtPushDataService pushDataService;

    @Resource
    private SecurityDataService securityDataService;

    @Override
    public void handle(HtPushData data) {
        try{
            String content = data.getValue();
            HongTuPushData pushData = JSON.parseObject(content,HongTuPushData.class);
            SecurityPushData securityData = JSON.parseObject(pushData.getData(), SecurityPushData.class);
            if(data.getStatus()==0){
                this.saveData(securityData);
            }
            data.setStatus(1);
        }catch (Exception e){
            data.setFailReason(ExceptionUtils.getMessage(e));
            data.setStatus(2);
        }finally {
            pushDataService.saveOrUpdate(data);
        }

    }

    @Override
    public void saveData(SecurityPushData data) {
        SecurityData securityData = new SecurityData();
        BeanUtil.copyProperties(data,securityData);

//        if(ObjectUtil.isNotNull(securityData.getCaptureImageUrl()) && securityData.getCaptureImageUrl().startsWith("http") ){
//            securityData.setCaptureImageUrl( getImageBase64(securityData.getCaptureImageUrl())  );
//        }
//        if(ObjectUtil.isNotNull(securityData.getGroupImageUrl()) && securityData.getGroupImageUrl().startsWith("http") ){
//            securityData.setGroupImageUrl( getImageBase64(securityData.getGroupImageUrl())  );
//        }
//        if(ObjectUtil.isNotNull(securityData.getFrameUrl()) && securityData.getFrameUrl().startsWith("http") ){
//            securityData.setFrameUrl( getImageBase64(securityData.getFrameUrl())  );
//        }
        securityData.setPushFlag(false);
        securityDataService.save(securityData);
    }



}
